<script setup lang="ts">
import HomeNarbar from '../index/components/HomeNavbar.vue'
import HomeCategory from './components/HomeCategory.vue'
import HotPanel from './components/HotPanel.vue'
import mmmGuess from '@/components/mmmGuess.vue'
import Skeleton from '@/components/Skeleton.vue'
import { getHomeBannerApi, getHomeCategoryApi, getHomeHotApi } from '@/services/home'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
const list = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
const getHomeBannerDate = async () => {
  const res = await getHomeBannerApi()
  console.log('home轮播', res)
  list.value = res.result
}
const getHomeCategoryDate = async () => {
  const res = await getHomeCategoryApi()
  console.log('home分类', res)
  categoryList.value = res.result
}
const getHomeHotData = async () => {
  const res = await getHomeHotApi()
  console.log('home热门', res)
  hotList.value = res.result
}
const onScrolltolower = () => {
  console.log('滚动到底部')
  mmmGuessInstance.value?.getMoreData()
}
const mmmGuessInstance = ref<InstanceType<typeof mmmGuess>>()
const onRefresherrefresh = async () => {
  isTriggered.value = true
  mmmGuessInstance.value?.resetData()
  await Promise.all([
    getHomeBannerDate(),
    getHomeCategoryDate(),
    getHomeHotData(),
    mmmGuessInstance.value?.getMoreData(),
  ])
  isTriggered.value = false
  console.log('下拉刷新被触发了')
}
const isTriggered = ref<boolean>(false)
const isLoading = ref(true)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerDate(), getHomeCategoryDate(), getHomeHotData()])
  isLoading.value = false
})
</script>

<template>
  <HomeNarbar />
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    refresher-default-style="white"
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <Skeleton v-if="isLoading" />
    <template v-else>
      <mmmSwiper :list="list" />
      <HomeCategory :categoryList="categoryList" />
      <HotPanel :hotList="hotList" />
      <mmmGuess ref="mmmGuessInstance" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: black;
  height: 100%;
}
.scroll-view {
  flex: 1;
}
</style>
